package top.lilele.adminSystem.web;

import cn.dev33.satoken.stp.StpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import top.lilele.adminSystem.dto.IdParams;
import top.lilele.adminSystem.dto.UserMenuDto;
import top.lilele.adminSystem.entity.AdminResourceEntity;
import top.lilele.adminSystem.entity.AdminRoleResourceEntity;
import top.lilele.adminSystem.exception.business.LoseIcoException;
import top.lilele.adminSystem.service.AdminResourceService;
import top.lilele.adminSystem.utils.BaseResponse;
import top.lilele.adminSystem.utils.ResponseCodeEnum;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author lilele
 */
@RestController
@RequestMapping(value = "resource")
@Api(tags = "资源管理")
public class AdminResourceController {
    @Resource
    AdminResourceService adminResourceService;

    /**
     * 新增菜单
     *
     * @param adminResourceEntity
     * @return
     */
    @ApiOperation(value = "新增资源")
    @SneakyThrows
    @PostMapping(value = "add")
    public BaseResponse add(@Validated @RequestBody AdminResourceEntity adminResourceEntity) {
        this.verifyIcon(adminResourceEntity);
        adminResourceEntity.setAvailable(1);
        adminResourceService.add(adminResourceEntity);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    @SneakyThrows
    public void verifyIcon(AdminResourceEntity adminResourceEntity) {
        if (adminResourceEntity.getMenuType() == 0) {
            boolean empty = StringUtils.isEmpty(adminResourceEntity.getIco());
            if (empty) {
                throw new LoseIcoException("资源类型缺失图标");
            }
        }
    }

    @ApiOperation(value = "根据id更新资源信息")
    @SneakyThrows
    @PostMapping(value = "updateResourceById")
    public BaseResponse updateResourceById(@RequestBody @Validated AdminResourceEntity adminResourceEntity) {
        this.verifyIcon(adminResourceEntity);
        adminResourceEntity.setAvailable(1);
        adminResourceService.updateResourceById(adminResourceEntity);
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }


    /**
     * 查询对应用户角色-菜单列表（登录用户都可使用）
     *
     * @return
     */
    @ApiOperation(value = "查询资源详情")
    @PostMapping(value = "selectMenuListByRoleAndUserId")
    public BaseResponse selectMenuListByUserId() {
        String id = StpUtil.getLoginIdAsString();
        List<UserMenuDto> list = adminResourceService.selectMenuListByRoleAndUserId(id);
        return BaseResponse.info(ResponseCodeEnum.Success, list);
    }

    /**
     * 根据id禁用资源
     *
     * @param idParams
     * @return
     */
    @ApiOperation(value = "根据id禁用资源")
    @PostMapping(value = "disableResourceById")
    public BaseResponse disableResourceById(@RequestBody @Validated IdParams idParams) {
        adminResourceService.disableResourceById(idParams.getId());
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    /**
     * 根据id开启资源
     *
     * @param idParams
     * @return
     */
    @ApiOperation(value = "根据id开启资源")
    @PostMapping(value = "enableResourceById")
    public BaseResponse enableResourceById(@RequestBody @Validated IdParams idParams) {
        adminResourceService.enableResourceById(idParams.getId());
        return BaseResponse.info(ResponseCodeEnum.Success, null);
    }

    /**
     * 根据当前登录用户获取接口资源，用于按钮权限控制（登录用户都可使用）
     *
     * @return
     */
    @ApiOperation(value = "获取当前用户接口资源")
    @PostMapping(value = "selectApiResourceByUserId")
    public BaseResponse selectApiResourceByUserId() {
        String id = StpUtil.getLoginIdAsString();
        List<UserMenuDto> list = adminResourceService.selectApiResourceByUserId(id);
        return BaseResponse.info(ResponseCodeEnum.Success, list);
    }

    /**
     * 查询所有的资源信息
     *
     * @param
     * @return
     */
    @ApiOperation(value = "查询资源列表")
    @PostMapping(value = "selectAllResources")
    public BaseResponse selectAllResources() {
        List<AdminResourceEntity> list = adminResourceService.selectAllResources();
        return BaseResponse.info(ResponseCodeEnum.Success, list);
    }

    /**
     * 根据id查询资源信息
     *
     * @param idParams
     * @return
     */
    @ApiOperation(value = "根据id查询资源详情")
    @PostMapping(value = "queryResourceById")
    public BaseResponse queryResourceById(@Validated @RequestBody IdParams idParams) {
        AdminResourceEntity info = adminResourceService.queryResourceById(idParams.getId());
        return BaseResponse.info(ResponseCodeEnum.Success, info);
    }

    /**
     * 根据角色id查询资源信息
     *
     * @param idParams
     * @return
     */
    @ApiOperation(value = "根据角色id查询资源信息")
    @PostMapping(value = "queryResourceByRoleId")
    public BaseResponse queryResourceByRoleId(@RequestBody @Validated IdParams idParams) {
        List<AdminRoleResourceEntity> list = adminResourceService.queryResourceByRoleId(idParams.getId());
        return BaseResponse.info(ResponseCodeEnum.Success, list);
    }
}
